package com.ryan.security.core.validate.code;

import com.ryan.security.core.properties.SecurityProperties;
import lombok.Data;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created by kaimin on 2/1/2019.
 * time : 22:13
 */
//有了代码生成器，则控制器里面的代码就可以替换掉了
@Data
@Component("smsCodeValidatorCodeGenerator")
public class SmsCodeValidatorCodeGenerator implements ValidatorCodeGenerator {

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    public ValidatorCode createImageCode(ServletWebRequest request) {
        String code = RandomStringUtils.randomNumeric(securityProperties.getCode().getSms().getLength());
        return new ValidatorCode(code, securityProperties.getCode().getSms().getExpireIn());
    }
}
